package ecs.dogear.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class CommenterDatabase {
	private static Map<Integer, User> itsUsers = new HashMap<Integer, User>();
	private static Map<Integer, ArrayList<Integer>> commentsOfUsers = new HashMap<Integer, ArrayList<Integer>>();
	private static Map<Integer, Comment> itsComments = new HashMap<Integer, Comment>();
	private static Map<Integer, ArrayList<Integer>> repliesOfComments = new HashMap<Integer, ArrayList<Integer>>();
	private static Map<String, ArrayList<Integer>> commentsOfPages = new HashMap<String, ArrayList<Integer>>();

	public static void addUser(int userId, User user) {
		itsUsers.put(userId, user);
	}

	public static User getUser(int userId) {
		return itsUsers.get(userId);
	}

	public static void addComment(Comment comment) {
		int userId = comment.getUserId();
		int commentId = comment.getCommentId();
		if (null != itsUsers.get(userId)) {
			// relate comments and user
			ArrayList<Integer> commentIds = commentsOfUsers.get(userId);
			if (null == commentIds) {
				commentIds = new ArrayList<Integer>();
				commentsOfUsers.put(userId, commentIds);
			}
			commentIds.add(commentId);
			itsComments.put(commentId, comment);
			// relate comments and url
			if (!"".equals(comment.getUrl())) {
				String url = comment.getUrl();
				ArrayList<Integer> ids = commentsOfPages.get(url);
				if (null == ids) {
					ids = new ArrayList<Integer>();
					commentsOfPages.put(url, ids);
				}
				ids.add(commentId);
			}
		}
	}

	public static ArrayList<Comment> getCommentsOf(int userId) {
		ArrayList<Comment> comments = new ArrayList<Comment>();
		ArrayList<Integer> commentIds = commentsOfUsers.get(userId);
		if (null == commentIds)
			return new ArrayList<Comment>();
		for (int id : commentIds) {
			Comment comment = itsComments.get(id);
			comments.add(comment);
		}
		return comments;
	}

	public static Comment getComment(int commentId) {
		return itsComments.get(commentId);
	}

	public static ArrayList<Comment> getRepliesOf(int commentId) {
		ArrayList<Comment> comments = new ArrayList<Comment>();
		ArrayList<Integer> replyIds = repliesOfComments.get(commentId);
		if (null == replyIds)
			return new ArrayList<Comment>();
		for (int id : replyIds) {
			Comment comment = itsComments.get(id);
			comments.add(comment);
		}
		return comments;
	}

	public static void addReplyTo(int commentId, int replyId) {
		if (null != itsComments.get(commentId)) {
			// relate replies and comment
			ArrayList<Integer> replyIds = repliesOfComments.get(commentId);
			if (null == replyIds) {
				replyIds = new ArrayList<Integer>();
				repliesOfComments.put(commentId, replyIds);
			}
			replyIds.add(replyId);
		}

	}

	public static ArrayList<Comment> getCommentsOfUrl(String pageUrl) {
		ArrayList<Comment> comments = new ArrayList<Comment>();
		ArrayList<Integer> commentIds = commentsOfPages.get(pageUrl);
		if (null == commentIds)
			return new ArrayList<Comment>();
		for (int id : commentIds) {
			// Comment comment = itsComments.get(id);
			Comment comment = getCommentWithReplies(id);
			comments.add(comment);
		}
		return comments;
	}

	private static Comment getCommentWithReplies(int commentId) {
		Comment comment = getComment(commentId);
		ArrayList<Integer> replyIds = repliesOfComments.get(commentId);
		if (null != replyIds) {
			for (int i = 0; i < replyIds.size(); i++) {
				comment.addReply(getCommentWithReplies(replyIds.get(i)));
			}
		}
		return comment;
	}

	public static Map<Integer, User> getAllUsers() {
		return itsUsers;
	}
	
}
